package com.persion.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.persion.common.annotation.Anonymous;
import com.persion.common.core.domain.AjaxResult;
import com.persion.common.utils.SecurityUtils;
import com.persion.common.utils.StringUtils;
import com.persion.domain.FrontCategory;
import com.persion.domain.Opeople;
import com.persion.dto.Params;
import com.persion.service.FrontCategoryService;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * (FrontCategory)表控制层
 *
 * @author WangFK
 * @since 2024-01-25 14:45:57
 */
@RestController
@Slf4j
@RequestMapping("/manage/category")
public class FrontCategoryController {
    /**
     * 服务对象
     */
    @Resource
    private FrontCategoryService frontCategoryService;

    @GetMapping("/getListCategortName")
    public AjaxResult getListCategortName() {
        return AjaxResult.success(frontCategoryService.list());
    }

    /**
     * 上下架分类状态
     *
     * @param frontCategory
     * @return
     */
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody FrontCategory frontCategory) {
        LambdaUpdateWrapper<FrontCategory> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(FrontCategory::getId, frontCategory.getId())
                .set(FrontCategory::getOpStatus, frontCategory.getOpStatus());
        return frontCategoryService.update(lqw) ? AjaxResult.success() : AjaxResult.warn("修改状态失败");
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public AjaxResult selectOne(@PathVariable Serializable id) {
        return AjaxResult.success(frontCategoryService.getById(id));
    }

    /**
     * 分页查询
     *
     * @return com.persion.common.core.domain.AjaxResult
     * @Author WangFK
     * @Date 2024/1/25 14:53
     * @Param [params]
     **/
    @Anonymous
    @GetMapping("/list")
    public AjaxResult list(Params params) {
        Page<FrontCategory> page = new Page<>(params.pageNum, params.pageSize);
        LambdaQueryWrapper<FrontCategory> lqw = new LambdaQueryWrapper<>();
        String search = params.getSearch();
        lqw.like(StringUtils.isNotEmpty(search), FrontCategory::getFrontName, search).or()
                .like(StringUtils.isNotEmpty(search), FrontCategory::getCreateBy, search).or()
                .like(StringUtils.isNotEmpty(search), FrontCategory::getUpdateBy, search)
                .eq(StringUtils.isNotEmpty(params.getOpStatus()), FrontCategory::getOpStatus, params.getOpStatus());
        if (ObjectUtil.isEmpty(params.getFlag())){
            lqw.eq(FrontCategory::getOpStatus,"0");
        }
        page = frontCategoryService.page(page, lqw);
        List<FrontCategory> records = page.getRecords();
        if (StringUtils.isEmpty(records)) {
            return AjaxResult.error("记录为空");
        }
        return AjaxResult.success()
                .put("rows", records).put("total", records.size());
    }

    /**
     * 新增数据
     *
     * @param frontCategory 实体对象
     * @return 新增结果
     */
    @PostMapping
    public AjaxResult insert(@RequestBody FrontCategory frontCategory) {
        if (StringUtils.isEmpty(frontCategory.getFrontName())) {
            return AjaxResult.error("请按规范填写！");
        }
        if (ObjectUtil.isNotEmpty(frontCategory.getId())) {
            return update(frontCategory);
        }
        try {
            frontCategory.setCreateBy(SecurityUtils.getUsername());
            frontCategoryService.save(frontCategory);
        } catch (Exception e) {
            return AjaxResult.error("分类名：" + frontCategory.getFrontName() + "重复！");
        }
        return AjaxResult.success("添加分类成功");
    }

    /**
     * 修改数据
     *
     * @param frontCategory 实体对象
     * @return 修改结果
     */
    @PutMapping
    public AjaxResult update(@RequestBody FrontCategory frontCategory) {
        FrontCategory byId = frontCategoryService.getById(frontCategory.getId());
        if (!frontCategory.getFrontName().equals(byId.getFrontName())) {
//            LambdaQueryWrapper<FrontCategory> lqw = new LambdaQueryWrapper<FrontCategory>().eq(FrontCategory::getFrontName, frontCategory.getFrontName());
//            List<FrontCategory> list = frontCategoryService.list(lqw);
//            if (StringUtils.isNotEmpty(list)){
//                return AjaxResult.error("分类名："+frontCategory.getFrontName()+"重复！");
//            }
            try {
                frontCategory.setUpdateBy(SecurityUtils.getUsername());
                frontCategoryService.updateById(frontCategory);
            } catch (Exception e) {
                return AjaxResult.error("分类名：" + frontCategory.getFrontName() + "重复！");
            }
        }
        return AjaxResult.success("修改分类成功");
    }

    /**
     * 删除数据
     *
     * @param id 主键结合
     * @return 删除结果
     */
    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable Serializable id) {
        return AjaxResult.success(frontCategoryService.removeById(id));
    }
}

